﻿/*****************************************
 * Export and Import is written by peter *
 * Necessary adjustment for Sims3Package *
 * Library is made.                      *
 * ***************************************/
using System;
using System.Windows.Forms;
using System.IO;
using Sims3Package.Wrapper;
using System.Reflection;

namespace PackageEditor.Helper
{
    public partial class S3SAHelper : Form
    {
        public S3SAResourceWrapper w;
        public S3SAHelper(Stream s)
        {
            InitializeComponent();
            w = new S3SAResourceWrapper(s);
            if (w.UnParse() != null) { lblLength.Text = string.Format("Length: {0}", w.UnParse().Length); }
            else { lblLength.Text = "Resource is null"; btnExport.Enabled = false; }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog save = new SaveFileDialog())
            {
                save.Filter = ".NET Assembly|*.dll";
                
                byte[] buffer = new byte[w.Assembly.BaseStream.Length];
                w.Assembly.BaseStream.Read(buffer, 0, buffer.Length);
                save.FileName = (Assembly.Load(buffer).FullName.Split(new char[] { ',' })[0] + ".dll");
                if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    using(FileStream s = new FileStream(save.FileName, FileMode.Create))
                    {
                        BinaryWriter rw = new BinaryWriter(s);
                        rw.Write(buffer);
                    }
                }

            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;

        }

        private void btnInport_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog open = new OpenFileDialog())
            {
                open.Filter = ".NET Assembly|*.dll";
                if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    using (FileStream stream = new FileStream(open.FileName, FileMode.Open))
                    {
                        w.Assembly = new BinaryReader(stream);
                    }
                }
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;

        }

        private void S3SAHelper_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.Icon;
        }
    }
}
